class Api::V1::OrderItemsController < Api::V1::BaseController

  def index
    @order_items_by_shops = @current_user.carts.joins(:spu => :shop).group_by{ |o| o.spu.shop.name }
  end

  def show
    @order_item = OrderItem.find params[:id]
    @service_url = URI.encode("http://www.hnxfsy.cn/mobile/chats?shop_name=#{@order_item.order.shop.blank? ? "" : @order_item.order.shop.name}&shop_id=#{@order_item.order.shop_id}&order_id=#{@order_item.order.id}&token=#{request.headers['Authorization'].split(' ').last}")
  end

  def create
    if params[:sku_id].blank?
      render json: { result: false, message: '缺少参数.' }, status: :ok and return
    end
    @sku = Sku.find params[:sku_id]
    if @sku.blank?
      render json: { result: false, message: '商品不存在.' }, status: :ok and return
    end
    @order_item = @current_user.order_items.where(on_cart: true, sku_id: @sku.id).first
    if @order_item.blank?
      @order_item = OrderItem.new
      @order_item.user_id = @current_user.id
      @order_item.sku_id = @sku.id
      @order_item.spu_id = @sku.spu.id

      sale_price = @sku.spu.promotion ? @sku.promotion_price : @sku.sale_price
      if not @current_user.membership.blank?
        sale_price = sale_price * @current_user.membership.discount / 100
      end
      
      @order_item.spu_name = @sku.spu.name
      @order_item.fee = sale_price
      @order_item.on_cart = true
      @order_item.quantity = params[:quantity].to_i
      @order_item.option_value_presentation = "规格:"
      @sku.sku_option_values.each do |sku_option_value|
        @order_item.option_value_presentation += "#{sku_option_value.option_value.presentation}、"
      end
    else
      @order_item.quantity += params[:quantity].to_i
    end
    if @order_item.save!
      render json: { result: true, message: '添加成功.', data: { id: @order_item.id } }, status: :ok
    else
      render json: { result: false, message: '添加失败.' }, status: :ok
    end
  end

  def update
    @order_item = OrderItem.find params[:id]
    if @order_item.on_cart
      @order_item.quantity = params[:quantity].to_i
      if @order_item.quantity < 1
        render json: { result: false, message: '数量不能少于1.' }, status: :ok and return
      end
      if @order_item.save
        render json: { result: true, message: '更新成功.' }, status: :ok
      else
        render json: { result: false, message: '更新失败.' }, status: :ok
      end
    else
      render json: { result: false, message: '该商品已结算.' }, status: :ok
    end
  end

  def destroy
    @order_item = OrderItem.find params[:id]
    if @order_item.on_cart
      if @order_item.destroy
        render json: { result: true, message: '删除成功.' }, status: :ok
      else
        render json: { result: false, message: '删除失败.' }, status: :ok
      end
    else
      render json: { result: false, message: '该商品已结算.' }, status: :ok
    end
  end

  def after_sales
    @order_items = OrderItem.where(user_id: @current_user.id, on_cart: false).where("state = 4 OR state = 5 OR state = 6").page(params[:page]).per(params[:per])
  end

  def after_sale
    @order_item = OrderItem.find params[:id]
    if @order_item.sent? || @order_item.completed?
      if @order_item.update order_item_params
        @order_item.update(state: :after_sale)
        render json: { result: true, message: '申请售后成功.' }, status: :ok
      else
        render json: { result: false, message: '申请售后失败.' }, status: :ok
      end
    else
      render json: { result: false, message: '该商品不能申请售后.' }, status: :ok
    end
  end

  private

  def order_item_params
    params.require(:order_item).permit(:after_sale_type, :refund_reason_id, :refund_fee, :refund_remark, refund_pictures_ids: [])
  end
  

end
